package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.EquipmentInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.EquipmentInfoRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class EquipmentInfoService {

    @Autowired
    EquipmentInfoRepository equipmentInfoRepository;

    public Page<EquipmentInfo> findAllEquipmentInfo(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<EquipmentInfo> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<EquipmentInfo> page = equipmentInfoRepository.findAll(mySpecifications, pageable);
        return page;
    }

    public Page<EquipmentInfo> findAllEquipmentInfo(int pageNum, HttpServletRequest request,int pageSize) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<EquipmentInfo> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
        Page<EquipmentInfo> page = equipmentInfoRepository.findAll(mySpecifications, pageable);
        return page;
    }
    //保存
    public void saveEquipmentInfo(String id, String code, String name, Integer objName, Integer source, String status, String statusExpression, Integer isShow) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        EquipmentInfo equipmentInfo = new EquipmentInfo();
        String uuid = "";
        if (id != null) {
            equipmentInfo = findById(id);
            equipmentInfo.setUpdateTime(new Date());
            equipmentInfo.setCreateUser(new User(shiroUser.getId()));
        }  {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            equipmentInfo.setId(uuid);
            equipmentInfo.setCreateTime(date);
            equipmentInfo.setCreateUser(new User(shiroUser.getId()));
            equipmentInfo.setIsValid(1);
        }
            equipmentInfo.setCode(code);
            equipmentInfo.setName(name);
            equipmentInfo.setObjName(objName);
            equipmentInfo.setSource(source);
            if(isShow == null){
                isShow=0;
            }
            equipmentInfo.setIsShow(isShow);
            /**后台过滤**/
            String sta = status;
            String stat = sta.replaceAll(",","");
            if(stat !=null){
                //添加字符串拼接效果,前端展示用
                String falg1 = "0=";
                String falg2 = "1=";
                String falg3 = "2=";
                String falg4 = "3=";
                //定义空字符串放截好的元素
                String one="";
                String two="";
                String three = "";
                String four = "";
                //截取长度为2
                if(stat.length()==2){
                    one=falg1+stat;
                    equipmentInfo.setStatus(one);
                    equipmentInfo.setStatusExpression("0");
                }
                //截取长度为4
                if (stat.length()==4){
                    one=falg1+stat.substring(0,2);
                    two=falg2+stat.substring(2,4);
                    String ot = one+"|"+two;
                    equipmentInfo.setStatus(ot);
                    equipmentInfo.setStatusExpression("0,1");
                }
                //截取长度为6
                if(stat.length() ==6){
                    one=falg1+stat.substring(0,2);
                    two=falg2+stat.substring(2,4);
                    three=falg3+stat.substring(4,6);
                    String ott = one + "|" +two+ "|"+three;
                    equipmentInfo.setStatus(ott);
                    equipmentInfo.setStatusExpression("0,1,2");
                }
                //截取长度为8
                if(stat.length()==8){
                    one=falg1+stat.substring(0,2);
                    two=falg2+stat.substring(2,4);
                    three=falg3+stat.substring(4,6);
                    four=falg4+stat.substring(6,8);
                    String ottf = one + "|" +two+ "|"+three+"|"+four;
                    equipmentInfo.setStatus(ottf);
                    equipmentInfo.setStatusExpression("0,1,2,3");
                }

            }
            equipmentInfoRepository.save(equipmentInfo);
    }
    //编辑框回显数据
    public EquipmentInfo findById(String id) {
        EquipmentInfo code = equipmentInfoRepository.findOne(id);
        return code;
    }
    //删除
    public void delEquipmentInfo(String[] ids) {
        List<EquipmentInfo> equipmentInfos = equipmentInfoRepository.findAll(Arrays.asList(ids));
        for (EquipmentInfo eq: equipmentInfos) {
            eq.setIsValid(0);
        }
        equipmentInfoRepository.save(equipmentInfos);
    }
    //导出
    public List<EquipmentInfo> findAllEquipmentInfos() {
        return equipmentInfoRepository.findByisValid();
    }
    //导入
    public String saveImport(List<String[]> list,String flag) {
        List<EquipmentInfo> allCode = equipmentInfoRepository.findCode();
        flag = "0";
        //遍历所有Excel的数据
        for (String[] info : list) {
                if(allCode.contains(info[0])){
                    flag = "1";
                    return flag;
                }

            EquipmentInfo einfo = new EquipmentInfo();
            einfo.setCode(info[0]);
            einfo.setName(info[1]);
            einfo.setStatus((info[2]));
            einfo.setStatusExpression(info[3]);
            einfo.setObjName(Integer.valueOf(info[4]));
            einfo.setSource(Integer.valueOf(info[5]));
            einfo.setIsShow(Integer.valueOf(info[6]));


            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            einfo.setId(uuid);
            einfo.setCreateTime(new Date());
            einfo.setCreateUser(new User(shiroUser.getId()));
            einfo.setIsValid(1);
            equipmentInfoRepository.save(einfo);
        }
        return flag;
    }

    /**
     * 查询code
     * @return
     */
    public List<EquipmentInfo> findCode() {
        return  equipmentInfoRepository.findCode();
    }
}
